// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
  // env:'wccloud-fm7zt',
  traceUser: true
});
const db = cloud.database();
const command = db.command;
const wxContext = cloud.getWXContext();
// 云函数入口函数
exports.main = async (event, context) => {
  var docid = event.docid;
  var filepath = event.filepath;
  var get_newnotice = event.get_newnotice;
  var state = event.state;
  var wokername = event.wokername;
  var receiveorder_time = event.receiveorder_time;
  var finishtime = event.finishtime;
  var picfile = event.picfile;
  var evaluate_date = event.evaluate_date;
  var evaluate_detail = event.evaluate_detail;
  var evaluate_user = event.evaluate_user;
  var evaluate_the_worker = event.evaluate_the_worker;

  if (event.options == 'announcement') {
    await db.collection('announcement').doc(docid).update({
      data: {
        desc: command.set(get_newnotice)
      }
    })
  } else if(event.options == 'add_wxuser'){
    var wxname = event.wxname;
    var avatar = event.avatar;
    await db.collection('users').add({
      data:{
        avatar:avatar,
        password:'',
        role:'user',
        telephone:'',
        username:wxname,
      }
    })
  }else if (event.options == 'delpic') {
    await db.collection('news').doc(docid).update({
      data: {
        pics: command.pull(filepath)
      }
    })
  } else if (event.options == 'pickorder') {
    await db.collection('fix_desc').doc(docid).update({
      data: {
        fix_state: command.set(state),
        worker: command.set(wokername),
        receiveorder_time: command.set(receiveorder_time),
      }
    })
  } else if (event.options == 'finishfix') {
    await db.collection('fix_desc').doc(docid).update({
      data: {
        fix_state: command.set(state),
        fix_finishtime: command.set(finishtime),
      }
    })
  } else if (event.options == 'cancel_order') {
    await db.collection('fix_desc').where({
      _id: docid
    }).remove()
    await cloud.deleteFile({
      fileList: picfile
    })
  } else if (event.options == 'evaluate') {
    const userpic = event.evaluate_user_pic;
    await db.collection('users').where({
      name: evaluate_the_worker
    }).update({
      data: {
        evaluate: command.push({
          each: [{
            date: evaluate_date,
            detail: evaluate_detail,
            user: evaluate_user,
            userpic:userpic,
          }]
        })
      }
    })
  } else if (event.options == 'editworker') {
    var truename = event.truename;
    var username = event.username;
    var password = event.password;
    var sex = event.sex;
    var age = event.age;
    var tel = event.tel;
    var desc = event.desc;
    var docid = event.docid;
    await db.collection('users').where({
      _id: docid,
    }).update({
      data: {
        name: command.set(truename),
        username: command.set(username),
        password: command.set(password),
        sex: command.set(sex),
        age: command.set(age),
        tel: command.set(tel),
        desc: command.set(desc),
      }
    }).then(res => {
      console.log(res);
    })
  } else if (event.options == 'delete_worker') {
    let fileid = event.pics
    await db.collection("users").doc(event.docid).remove();
    await cloud.deleteFile({
      fileList: [fileid],
    })
  }else if(event.options == 'update_avatar'){
    var filepath = event.filepath;
    var username = event.username;
    var nowlogin_user = event.nowlogin_user;
    var oldavatar = event.oldavatar;
    console.log(oldavatar);
    await cloud.deleteFile({
      fileList: [oldavatar],
    })
    await db.collection('users').where(command.and([
      {
        _openid: nowlogin_user
      },
      {
        username: username
      }
    ])).update({
      data:{
        avatar:command.set(filepath)
      }
    })
  }else if(event.options == 'update_password'){
    var username = event.username;
    var newpassword = event.newpassword;
    await db.collection('users').where({
      username:username,
    }).update({
      data:{
        password:command.set(newpassword)
      }
    }).then(res=>{
      console.log(res);
    })
  }else if(event.options == 'send_message'){
    var userA = event.userA;
    var userB = event.userB;
    var detail = event.detail;
    await db.collection('chatroom').where(command.and([
      {
        userA:userA,
      },
      {
        userB:userB,
      }
    ]))
    .update({
      data:{
        detail:command.push(detail)
      }
    })
    .then(res=>{
      console.log(res);
    })
  }else if(event.options == 'send_message1'){
    var userA = event.userA;
    var userB = event.userB;
    var detail = event.detail;
    await db.collection('chatroom').where(command.and([
      {
        userA:userB,
      },
      {
        userB:userA,
      }
    ]))
    .update({
      data:{
        detail:command.push(detail)
      }
    })
    .then(res=>{
      console.log(res);
    })
  }else if(event.options == 'del_chatmessage'){
    var userA = event.userA;
    var userB = event.userB;
    db.collection('chatroom').where(command.and([
      {
        userA:userA,
      },
      {
        userB:userB,
      }
    ])).remove().then(res=>{
      console.log(res);
    })
  }



  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}